import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

/*
 * Created by Jesse Phillips
 * Created on Mar 15, 2006
 */
/**
 * This program uses a binary tree to hold a list of words.
 * It will then print them out using three traversials.
 * Post, In order, and Pre.
 * 
 * <p><b>
 * Extra Credit:
 * </b><pre>
 * Attempted extra credit not yet stated.
 * </pre><b>
 * 
 * History:
 * </b><pre>
 * Mar 15, 2006: Project started. Only missing comments.
 * Mar 16, 2006: Added comments
 * </pre>
 * 
 * @author Jesse Phillips
 * @see "No barrowed code."
 */
public class BTDriver {
	private Scanner input;
	public PrintWriter output;
	private BinaryTree BT;
	
	public static void main(String[] args) {
		try {
			BTDriver app = new BTDriver("testinput.txt", "testoutput.txt");
			System.out.println("Building tree...");
			app.addToTree();
			
			System.out.println("Printing Preorder...");
			app.output.println("Contents preorder:");
			app.printPreOrder(app.getBT());
			
			System.out.println("Printing inorder...");
			app.output.println("\nContents in order:");
			app.printInOrder(app.getBT());
			
			System.out.println("Printing postorder...");
			app.output.println("\nContents Postorder:");
			app.printPostOrder(app.getBT());
			
			app.close();
		} catch (FileNotFoundException e) {
			System.out.println(e);
		}
	}
	
	/**
	 * Gets the input and output files ready.
	 * 
	 * @param inFile Which file will be used for input.
	 * @param outFile Which file will be used for output.
	 * @throws FileNotFoundException Thrown when it recieves the
	 * 		exception itself.
	 */
	BTDriver(String inFile, String outFile) throws FileNotFoundException {
		try {
			input = new Scanner(new FileReader(inFile));
			output = new PrintWriter(new FileWriter(outFile));
		}catch(FileNotFoundException fnfe) {
			throw fnfe;
		} catch (IOException e) {
			System.out.println(e);
			System.exit(1);
		}
	}
	
	/**
	 * Returns the root binary tree.
	 * 
	 * @return A binaryTree that is the root of the text.
	 */
	public BinaryTree getBT() {
		return BT;
	}
	
	/**
	 * Adds a given element to the Binary Tree.
	 */
	public void addToTree() {
		BT = new BinaryTree(input.next());
		
		while(input.hasNext())
			BT.add(input.next());
	}
	
	/**
	 * Prints the contents of a given birary tree in order.
	 * 
	 * @param root Where you want to start the printing.
	 */
	public void printInOrder(BinaryTree root) {
		if(root == null)
			return;
		printInOrder(root.left);
		output.println(root.getItem());
		printInOrder(root.right);
	}
	
	/**
	 * Prints the contents of a given binary tree in preoder.
	 * 
	 * @param root Where you want to start printing.
	 */
	public void printPreOrder(BinaryTree root) {
/*ans bl*/
		if(root == null)
			return;
		output.println(root.getItem());
		printInOrder(root.left);
		printInOrder(root.right);
/*ans ab*/
	}
	
	/**
	 * Prints the contents of a given binary tree in post order.
	 * 
	 * @param root Where you want to start printing from.
	 */
	public void printPostOrder(BinaryTree root) {
/*ans bl*/
		if(root == null)
			return;
		printInOrder(root.left);
		printInOrder(root.right);
		output.println(root.getItem());
/*ans ab*/
	}
	
	/**
	 * Closes both the input and output file.
	 */
	public void close() {
		input.close();
		output.close();
	}
}
